<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/24 0024
 * Time: 下午 3:03
 */

namespace App\Model;


use App\Utility\RedisTools;
use App\Enum\SysEnum;
class GameType extends BaseModel
{
    //DB对象
    public $tableName = 'qc_game_type';
    public $tbRoomName = 'qc_game_room';
    public $tbRoomDisplayName = 'qc_game_display';
    public $tbAgentGameName = 'qc_agent_games';
    
    public function __construct(){
        parent::__construct();
    }
    
    
    /**
     * TODO 获取游戏列表；
     */
    public function getList($pagesize = 20,$allFlag = FALSE){
        //BaseEnum::Logger('---------------GameType --> getList--------conect--->'.json_encode($this->connect_db),'user-login');
        $fields = ["gameId","gameKey","gameName","showTxt","status"];
        $result = FALSE;
        if ($allFlag === TRUE) {
            $result = $this->getDb()->where('gameId',0,">")->get($this->tableName,$pagesize,$fields);
        }else{
            $result = $this->getDb()->where('status',1)->get($this->tableName,$pagesize,$fields);
        }
        //查询游戏列表数据内容；
        $this->clearPool(); //清掉连接池操作
        return $result;
    }
    
    /**
     * TODO 获取游戏详细；
     */
    public function getInfo($gameId,$getFj = true,$fjFields = false){
        $fields = ["gameId","gameKey","gameName","showTxt","status"];
        //查询游戏列表数据内容；
        $result = $this->getDb()->where('gameId',$gameId)->getOne($this->tableName,$fields);
        
        $chart =  (isset($result) && is_object($result))  ? "IsObject" : "isArray"; 
        
        //获取房间内容；
        if ($getFj === true && !empty($result)) {
            $result["rooms"] = [];
            $fjFields = !empty($fjFields) ? $fjFields : '*'  ;
            $fjList = $this->getDb()->where('gameId',$gameId)->where("status",1)->get($this->tbRoomName,10,$fjFields);
            $rooms = !empty($fjList) ? array_column($fjList, null ,"roomId"):false ;
            if(!empty($fjList) && !empty($rooms)) {
                foreach ($rooms as & $room) {
                    $room["smallPoints"] = isset($room["smallPoints"]) ? floatval($room["smallPoints"]) :0;             //准入值
                    $room["maxInPoints"] = isset($room["maxInPoints"]) ? floatval($room["maxInPoints"]) :0;             //最高准入值
                    $room["everyPoints"] = isset($room["everyPoints"]) ? floatval($room["everyPoints"]) :0;             //最大盲注值（扑克牌） ；
                    //TODO 处理不同游戏的数据(扑克牌)；
                    if ($gameId == 116) {
                        //加一个前注金额(游戏之前的金额)
                        $room["startInPoints"] = isset($room["startInPoints"]) ? floatval($room["startInPoints"]) :0 ; //前注金额
                        $room["minBlindPoints"] = floatval($room["everyPoints"] / 2) ; //最小盲注值
                    }
                }
                $result["rooms"] = $rooms;
            }
        }
        $this->clearPool(); //释放连接池
        return $result;
    }
    
    /**
     * TODO 获取当前游戏
     * @param int $gRoomId
     * @return boolean
     */
    public function getGameRoomDetail($gRoomId) {
        $allRoomkey = SysEnum::ONLINE_ROOMS_KEY."Rooms";
        $saveFJString = RedisTools::Pool_hGet($allRoomkey, $gRoomId); //取房间内容；
        $fjDetail = !empty($saveFJString) ? json_decode($saveFJString,TRUE): false ;
        if(!$fjDetail){
            $fjDetail = $this->getDb()->where('roomId',$gRoomId)->where("status",1)->getOne($this->tbRoomName);
            //TODO 修改字段把筹码：限线字段值统一放在字段中
            if(!empty($fjDetail) && isset($fjDetail['roomSeting'])){
                $fjDetail["roomSeting"] = !empty($fjDetail['roomSeting']) ? json_decode($fjDetail['roomSeting'],TRUE):[]; //其它配置值
            }
            if (isset($fjDetail) && !empty($fjDetail)) {
                RedisTools::Pool_hSet($allRoomkey, $gRoomId,json_encode($fjDetail));    //保存进入缓存
            }
            $this->clearPool(); //释放连接池
        }
        return $fjDetail;
    }
    
    
    /**
     * 获取玩家开桌信息Show表信息
     * @param $roomNo 根据房间号$roomNo来获取桌台的信息 
     */
    public function getGameTableDetail($gameNo,$openStatus = false) {
        if(!$gameNo) return false ;
        if($openStatus === false){
            $fjDetail = $this->getDb()->where('gameNo',$gameNo)->getOne($this->tbRoomDisplayName);
        }else{
            $fjDetail = $this->getDb()->where('gameNo',$gameNo)->where("status",$openStatus)->getOne($this->tbRoomDisplayName);
        }
        $this->clearPool();
        return $fjDetail;
    }
    
    
    
    /**
     * 创建游戏房间数据；
     * 
     * @param int $gameRId
     */
    public function createRoomsShow($GID,$gameRId,$GF_ID=null,$deatil = false) {
        //房间ID目录下是否有还未开局，但人数不够，等待中的桌号;
        $tbName = $this->tbRoomDisplayName;
        //创建一行；
        $oneGame = array_merge(["gameNo"=> $GF_ID, "roomId"=> $gameRId, "gameId"=> $GID] ,$deatil );
        //不能依赖数据库的DB值； 
        $ifok = $this->getDb() -> insert($tbName,$oneGame);
        $this->clearPool();
        return $ifok ;
    }
    
    
    /**
     * 修改房间数据
     * @param string $roomId
     * @param boolean $editdata
     */
    public function editRoomShow($roomId,$editdata = false,$field = '') {
        if(!$roomId || !$editdata) return false ;
        if(!$field) $field = 'gameNo';
        $res = $this->getDb() ->where($field, $roomId)->update($this->tbRoomDisplayName, $editdata) ;
        $this->clearPool();
        return $res;
    }
    
    
    
    /**
     * TODO 获取代理下边的游戏列表
     * @param number $agentId
     */
    public function getAgentGames($agentId = 0) {
        $fields = ["gameId","agentId","gameStatus"];
        $result = $this->getDb()->where('agentId',$agentId,"=")->get($this->tbAgentGameName,50,$fields);
        //查询游戏列表数据内容；
        $this->clearPool(); //清掉连接池操作
        return $result;
    }
    
}



